package com.springboot.dxssglxt.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.dxssglxt.entity.Menu;
import com.springboot.dxssglxt.mapper.MenuMapper;
import com.springboot.dxssglxt.service.IMenuService;
import com.springboot.dxssglxt.util.BuildTree;
import com.springboot.dxssglxt.util.Result;
import com.springboot.dxssglxt.util.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-05-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    @Autowired
    MenuMapper sysMenuMapper;


    /**
     * 获取当前用户树结构
     * @return
     */
    @Transactional
    @Override
    public List<Tree<Menu>> listMenuTree(Integer id) {
        List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
		/*List<Menu> menuDOs =null;
		String redismenu= redisUtils.getKey("menu"+id);
		if(StringUtils.isEmpty(redismenu)){
			menuDOs=sysMenuMapper.listMenuByAccountId(id);
			redisUtils.setKey("menu"+id, JSONArray.toJSONString(menuDOs),new Long(30), TimeUnit.DAYS);
		}else{
			menuDOs=JSONArray.parseArray(redismenu,Menu.class);
		}*/
        List<Menu> menuDOs =sysMenuMapper.listMenuByAccountId(id);
        for (Menu sysMenuDO : menuDOs) {
            Tree<Menu> tree = new Tree<Menu>();
            tree.setId(sysMenuDO.getMenuid().toString());
            tree.setParentId(sysMenuDO.getParentid().toString());
            tree.setText(sysMenuDO.getName());
            Map<String, Object> attributes = new HashMap(16);
            attributes.put("url", sysMenuDO.getUrl());
            attributes.put("icon", sysMenuDO.getIcon());
            attributes.put("target", sysMenuDO.getTarget());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<Menu>> list = BuildTree.buildList(trees, "0");
        return list;
    }

    /**
     * 获取所有的菜单列表
     * @return
     */
    @Transactional
    @Override
    public List<Menu> list() {
        return sysMenuMapper.list();
    }

    /**
     * 根据菜单获取菜单详细数据出错
     * @return
     */
    @Transactional
    @Override
    public Menu getMenuById(Integer menuid) {
        return this.sysMenuMapper.selectById(menuid);
    }

    /**
     * 根据菜单ID修改菜单
     * @param menu
     * @return
     */
    @Transactional
    @Override
    public Result update(Menu menu) {
        if(this.sysMenuMapper.updateById(menu)==1){
            return Result.reOk("修改成功");
        }
        return Result.reError("修改菜单出错");
    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @Transactional
    @Override
    public Result add(Menu menu) {
        if(menu.getParentid()!=null&&menu.getParentid()==0){
            menu.setIcon("layui-icon-home");//主菜单图标
        }else{
            menu.setIcon("layui-icon-form");//子菜单图标
        }
        menu.setCreatetime(new Date());
        menu.setUpdatetime(new Date());
        if(this.sysMenuMapper.insert(menu)==1){
            return Result.reOk("新增菜单成功");
        }
        return Result.reError("新增菜单出错");
    }

    /**
     * 删除菜单
     * @param menuid
     * @return
     */
    @Transactional
    @Override
    public Result remove(Integer menuid) {
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<Menu>();
        queryWrapper.eq("parentid",menuid);
        List<Menu> mlist=this.sysMenuMapper.selectList(queryWrapper);
        if(mlist!=null&&mlist.size()>=1){
            return Result.reError("抱歉，此菜单下面还包含了子菜单，请先删除子菜单再来进行删除");
        }
        if(this.sysMenuMapper.deleteById(menuid)==1){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除菜单出错");
    }

}
